﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkSummary.Business;
using WorkSummary.Bussiness.Dal;
using WorkSummary.Bussiness.Model;
using WorkSummary.Common;
using WorkSummary.Common.Data;
using WorkSummary.Common.log;
using WorkSummary.Common.Provider;

namespace WorkSummary.Bussiness.Bll
{
    public class BTask
    {
        public static BTask Instance
        {
            get { return SingletonProvider<BTask>.Instance; }
        }

        public TopResult AddTask(MTask r)
        {
            try
            {
                r.TaskID = Guid.NewGuid().ToString();
                r.CreateUserID = SysVisitor.Instance.UserID;
                r.CreateTime = DateTime.Now;
                r.UserName = BUser.Instance.GetUserName(r.UserID);
                r.IsDelete = 0;
                DTask.Instance.Insert(r);
                log.info(SysVisitor.Instance.AccountNum, "新增任务", "新增任务:" + r.TaskID);
                return new TopResult();
            }
            catch (Exception ex)
            {
                log.error(SysVisitor.Instance.AccountNum, "新增任务", "新增任务出错", ex);
                return new TopResult(-1, ex.Message);
            }
        }

        public TopResult ModifyTask(MTask r)
        {
            try
            {
                var oldTask = DTask.Instance.Get("TaskID", r.TaskID);
                r.CreateTime = oldTask.CreateTime;
                r.CreateUserID = oldTask.CreateUserID;
                r.UserName = BUser.Instance.GetUserName(r.UserID);
                DTask.Instance.Update("TaskID", r);
                log.info(SysVisitor.Instance.AccountNum, "修改任务", "修改任务:" + r.TaskID + ";原任务:" + oldTask.TaskInfo + ";原备注:" + oldTask.Remark);
                return new TopResult();
            }
            catch (Exception ex)
            {
                log.error(SysVisitor.Instance.AccountNum, "修改任务", "修改任务出错", ex);
                return new TopResult(-1, ex.Message);
            }
        }

        public TopResult DelTask(string ids)
        {
            try
            {
                string sql = string.Format("update t_task set IsDelete='1' where TaskId in ({0})", ids);
                DbUtils.ExecuteNonQuery(sql);
                log.info(SysVisitor.Instance.AccountNum, "删除任务", "删除任务" + ids);
                return new TopResult();
            }
            catch (Exception ex)
            {
                log.error(SysVisitor.Instance.AccountNum, "删除任务", "删除任务出错", ex);
                return new TopResult(-1, ex.Message);
            }
        }

        public List<MTask> GetAllTaskByUser(string sdate, string edate)
        {
            string where = "IsDelete=0 and UserID = '" + SysVisitor.Instance.UserID + "'";
            if (!string.IsNullOrEmpty(sdate))
            {
                where += " and StartDate >='" + sdate + " 00:00:00'";
            }
            if (!string.IsNullOrEmpty(edate))
            {
                where += " and EndDate <='" + edate + " 23:59:59'";
            }
            var list = DTask.Instance.GetAll(where).ToList();
            return list;
        }
        public List<MTask> GetAllTask(string sdate, string edate)
        {
            string where = "IsDelete=0";
            if (!string.IsNullOrEmpty(sdate))
            {
                where += " and StartDate >='" + sdate + " 00:00:00'";
            }
            if (!string.IsNullOrEmpty(edate))
            {
                where += " and EndDate <='" + edate + " 23:59:59'";
            }
            var list = DTask.Instance.GetAll(where).ToList();
            return list;
        }

        public TopResult GetChartData()
        {
            string sql = @"SELECT tu.UserName,d,c FROM t_user tu LEFT JOIN 
(SELECT t.UserID,t.UserName,sum(d)+1 AS d,COUNT(t.UserID) AS c FROM (
SELECT UserID,UserName,DATEDIFF(EndDate,StartDate) AS d FROM t_task WHERE IsDelete=0 AND EndDate<date_sub(CURDATE(),interval -30 day)
) t GROUP BY UserID,UserName
) t2
 ON tu.UserID=t2.UserID ORDER BY d desc";
            var dt = DbUtils.GetTable(sql);
            return new TopResult(0, dt.Rows);
        }
        public TopResult GetChartDataWithDate(string s, string e)
        {
            string sql = string.Format(@"SELECT tu.UserName,d,c FROM t_user tu LEFT JOIN 
(SELECT t.UserID,t.UserName,sum(d)+1 AS d,COUNT(t.UserID) AS c FROM (
SELECT UserID,UserName,DATEDIFF(EndDate,StartDate) AS d FROM t_task WHERE IsDelete=0 AND EndDate>'{0}' AND StartDate<'{1}'
) t GROUP BY UserID,UserName
) t2
 ON tu.UserID=t2.UserID ORDER BY d desc", s, e);
            var dt = DbUtils.GetTable(sql);
            return new TopResult(0, dt.Rows);
        }

        public TopResult GetPieDataByUser(string userid)
        {
            string sql = string.Format("SELECT UserName,TaskInfo,DATEDIFF(EndDate,StartDate)+1 AS d FROM t_task WHERE IsDelete=0 AND UserID='{0}' AND EndDate<date_sub(CURDATE(),interval -30 day)", userid);
            var dt = DbUtils.GetTable(sql);
            return new TopResult(0, dt.Rows);
        }
    }
}
